package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzq.dao.pojo.*;
import com.wzq.exception.GlobleExceptionHandler;
import com.wzq.service.*;
import com.wzq.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Configuration
public class GetSome {
    @Autowired
    protected ISysnumberService iSysnumberService;
    @Autowired
    protected IWechat_userService iWechat_userService;
    @Autowired
    protected IProjectService iProjectService;
    @Autowired
    protected ISpaceService iSpaceService;
    @Autowired
    protected ITypeService iTypeService;
    @Autowired
    protected IOtherService iOtherService;
    @Autowired
    protected IAppointmentService iAppointmentService;
    @Autowired
    protected IAchievementService iAchievementService;
    @Autowired
    protected IMemberService iMemberService;
    @Autowired
    protected IMessageService iMessageService;
    @Autowired
    protected IDiscussService iDiscussService;
    @Autowired
    protected IMytrackService iMytrackService;
    @Autowired
    protected IDisableTimeService iDisableTimeService;
    @Autowired
    protected IP_parametersService ip_parametersService;
    @Autowired
    protected ITimeSelectionService iTimeSelectionService;
    @Autowired
    protected IDoorProjectService iDoorProjectService;
    @Autowired
    protected IImageService iImageService;
    @Autowired
    protected IDurationService iDurationService;
    @Autowired
    protected ISumService iSumService;
//    @Autowired
//    IFavoritesService iFavoritesService;

//    @Autowired
//    IOtherService iOtherService;
//    @Autowired
//    ISpaceService iSpaceService;
//    @Autowired
//    ISysnumberService iSysnumberService;
    // 封装 封面图片
    public List<Project> getPicUrl(List<Project> projectList) {
        List<Project> projects = new ArrayList<>();
        LambdaQueryWrapper<Image> lqw = new LambdaQueryWrapper<>();
        for (Project project : projectList) {
            lqw.clear();
            lqw.eq(Image::getP_id, project.getId()).eq(Image::getType, 1);
            project.setPicurl(iImageService.getOne(lqw).getUrl());
            projects.add(project);
        }
        return projects;
    }
    //  获取多个用户名称
    public List<String> getUsernames(List<Integer> ids) {

        List<String> s = new ArrayList<>();
        List<Wechat_user> list = iWechat_userService.listByIds(ids);//获取用户名
        for (Wechat_user user : list) {
            s.add(user.getUsername());
        }
        return s;
    }
    //  获取项目parent类型
    public String getsys_number(Integer id) {
        Wechat_user wechat_user = iWechat_userService.getById(id);//获取用户名
        return wechat_user.getSys_number();
    }
    //  获取单个用户名称
    public String getW_name(Integer id) {
        Wechat_user user = iWechat_userService.getById(id);//获取用户名
        return user.getUsername();
    }

    //    获取用户头像
    public String getAvatarUrl(Integer id) {
        Wechat_user user = iWechat_userService.getById(id);//获取用户名
        return user.getAvatarUrl();
    }

    //  获取多个项目名称
    public List<String> getTitles(List<Integer> ids) {
        List<String> s = new ArrayList<>();
        List<Project> projects = iProjectService.listByIds(ids);//获取用户名
        for (Project project : projects) {
            s.add(project.getTitle());
        }
        return s;
    }

    //  获取单个项目名称
    public String getP_name(Integer id) {
        Project project = iProjectService.getById(id);//获取用户名
        return project.getTitle();
    }

    //  获取多个类型名称
    public List<String> getT_types(List<Integer> ids) {
        List<String> s = new ArrayList<>();
        List<Type> types = iTypeService.listByIds(ids);//获取类型集合
        for (Type type : types) {
            s.add(type.getType());
        }
        return s;
    }

    //  获取单个项目名称
    public String getT_type(Integer id) {
        Type type = iTypeService.getById(id);//获取用户名
        return type.getType();
    }

    //  获取多个parent名称
    public List<Integer> getparents(List<Integer> ids) {
        List<Integer> s = new ArrayList<>();
        List<Type> types = iTypeService.listByIds(ids);//获取类型集合
        for (Type type : types) {
            s.add(type.getParent());
        }
        return s;
    }

    //  根据type id获取项目parent类型
    public Integer getparent(Integer id) {
        Type type = iTypeService.getById(id);//获取用户名
        return type.getParent();
    }

    //  根据type id获取项目parent类型
    public Integer getType_id(String title) {
        Type type = iTypeService.getOne(new LambdaQueryWrapper<Type>().eq(Type::getType,title));//获取用户名
        return type.getId();
    }

    //  获取所有w_id
    public List<Integer> getAllW_id() {
        List<Integer> list = new ArrayList();
        for (Wechat_user w : iWechat_userService.list()) {
            list.add(w.getId());
        }
        System.out.println(list);
        return list;
    }

    //  获取sys_number
    public String getSys_number(Integer id) {
        return  iSysnumberService.getById(id).getNumber();
    }

    //活动是否结束
    public void isover() throws Exception {
        LambdaQueryWrapper<Project> lqw = new LambdaQueryWrapper<Project>();
        lqw.eq(Project::getIsover, 0);
        List<Project> list = iProjectService.list(lqw);
        for (Project p : list) {
            if (p.getEndtime() != null && p.getEndtime().compareTo(DateUtil.getCurrentDatePath("yyyy-MM-dd hh:mm")) < 0) {
                p.setIsover(1);
                iProjectService.updateById(p);
            }
        }
    }

    // 返回禁用时间 根据key（年 ，年月 ，年月日 ）
    public List<DisableTime> formatAll(String key, Integer p_id, Integer... format) {
        //查询该年的禁用月份（包含日，时分）
        LambdaQueryWrapper<DisableTime> lqw = new LambdaQueryWrapper<>();
        if (format[0] > 0)
            lqw.in(DisableTime::getFormat, format);
        lqw.eq(DisableTime::getP_id, p_id).likeRight(DisableTime::getDisable_time, key);
        return iDisableTimeService.list(lqw);
    }

    // 月份加
    public String addmonth(String date) {
        int i = Integer.parseInt(date.substring("2002-".length(), "2002-".length() + 2)) + 1;
        System.out.println(i);
        if (i < 10)
            return date.substring(0, "2002-".length()) + "0" + i + date.substring("2002-02".length());
        else
            return date.substring(0, "2002-".length()) + i + date.substring("2002-02".length());
    }

    //  申请是否结束
    public void appointmentover() throws Exception {
        LambdaQueryWrapper<Appointment> lqw = new LambdaQueryWrapper<Appointment>();
        lqw.eq(Appointment::getIsover, 0).eq(Appointment::getPass,1);
        List<Appointment> list = iAppointmentService.list(lqw);
        for (Appointment appointment : list) {
            if (appointment.getEnd_time().compareTo(DateUtil.getCurrentDatePath("yyyy-MM-dd HH:mm")) < 0) {
                appointment.setIsover(1);
                iAppointmentService.updateById(appointment);
            }
        }
    }
    // 根据项目名模糊查询
    public  Integer[] searchByPname(String name){
        int i=0;
        LambdaQueryWrapper<Project> lqw = new LambdaQueryWrapper<>();
        lqw.select(Project::getId).like(Project::getTitle,name);
        List<Project> list = iProjectService.list(lqw);
        Integer[] integers = new Integer[list.size()];
        for (Project project  : list) {
            integers[i++]= project.getId();
        }
        return integers;
    }
    // 根据用户名模糊查询
    public  Integer[] searchByWname(String name){
        int i=0;
        LambdaQueryWrapper<Wechat_user> lqw = new LambdaQueryWrapper<>();
        lqw.select(Wechat_user::getId).like(Wechat_user::getUsername,name);
        List<Wechat_user> list = iWechat_userService.list(lqw);
        Integer[] integers = new Integer[list.size()];
        for (Wechat_user user  : list) {
            integers[i++]= user.getId();
        }
        System.out.println(integers);
        return integers;
    }
}
